package org.tourism.controller;

import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.tourism.VO.UserVO;
import org.tourism.common.CommonResponse;
import org.tourism.entity.User;
import org.tourism.service.UserService;

@RestController
@Validated
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @PostMapping("/tourist/login")
    @ResponseBody
    public CommonResponse<UserVO> login(@RequestParam("username") String username,
                                        @RequestParam("password") String password,
                                        HttpSession session) {
        if(username.isEmpty()) {
            System.out.println("用户名为空");
            return CommonResponse.createForError(1,"请输入用户名");
        }
        if(password.isEmpty()) {
            System.out.println("密码为空");
            return CommonResponse.createForError(1, "请输入密码");
        }
        UserVO userVO = userService.getUserByNameAndPassword(username, password);
        if(userVO == null) {
            return CommonResponse.createForError(1, "用户名或密码错误");
        }else{
            session.setAttribute("user", userVO);
            return CommonResponse.createForSuccess("登录成功", userVO);
        }
    }

    @PostMapping("/manager/login")
    @ResponseBody
    public CommonResponse<UserVO> login(@RequestParam("username") String username,
                                        @RequestParam("password") String password,
                                        @RequestParam("identity") String identity,
                                        HttpSession session){
        if(username.isEmpty()) {
            System.out.println("用户名为空");
            return CommonResponse.createForError(1,"请输入用户名");
        }
        if(password.isEmpty()) {
            System.out.println("密码为空");
            return CommonResponse.createForError(1, "请输入密码");
        }
        UserVO userVO = userService.getUserByNamePasswordIdentity(username, password, identity);
        if(userVO == null) {
            return CommonResponse.createForError(1, "用户名或密码错误");
        }else{
            session.setAttribute("user", userVO);
            return CommonResponse.createForSuccess("登录成功", userVO);
        }
    }

    @PostMapping("/tourist/register")
    @ResponseBody
    public CommonResponse<UserVO> register(@RequestBody UserVO userVO, HttpSession session){
        userVO.setIdentity("游客");
        String username = userVO.getUsername();
        String password = userVO.getPassword();
        String identity = userVO.getIdentity();
        if(userService.getUserByNamePasswordIdentity(username, password, identity) != null) {
            return CommonResponse.createForError(1, "用户已存在");
        }
        userService.addUser(userVO);
        userVO.setUid(userService.getUserByNamePasswordIdentity(username, password, identity).getUid());
        session.setAttribute("user", userVO);
        return CommonResponse.createForSuccess("注册成功", userVO);
    }
}
